﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace Insurance.ReadCard
{
    public class CardReadOut
    {
        public string CanOpen { set; get; }

        public string Message { set; get; }

        public bool Success { set; get; }
    }

    public class CardReader
    {
        /// <summary>
        /// 校验PIN读取社保卡信息接口
        /// </summary>
        /// <param name="DataIn"></param>
        /// <param name="DataOut"></param>
        /// <returns></returns>
        [DllImport("CardReaderDLL.dll", SetLastError = true, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        private static extern int ZJ_ReadCardInfo(
            [MarshalAs(UnmanagedType.LPArray)] byte[] DataIn,
            [MarshalAs(UnmanagedType.LPArray)] byte[] DataOut);

        /// <summary>
        /// 无PIN校验读取社保卡接口
        /// </summary>
        /// <param name="DataIn"></param>
        /// <param name="DataOut"></param>
        /// <returns></returns>
        [DllImport("CardReaderDLL.dll", SetLastError = true, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        private static extern int ZJ_ReadCardInfo_NoPin(
            [MarshalAs(UnmanagedType.LPArray)] byte[] DataIn,
            [MarshalAs(UnmanagedType.LPArray)] byte[] DataOut);

        /// <summary>
        /// 生成签名信息接口
        /// </summary>
        /// <param name="key"></param>
        /// <param name="secret"></param>
        /// <param name="unix_timestamp"></param>
        /// <param name="request_body"></param>
        /// <param name="outMsg"></param>
        /// <returns></returns>
        [DllImport("CardReaderDLL.dll", SetLastError = true, CharSet = CharSet.Ansi,
            CallingConvention = CallingConvention.StdCall)]
        private static extern int ZJ_Hmac_SM3([MarshalAs(UnmanagedType.LPArray)] byte[] key,
            [MarshalAs(UnmanagedType.LPArray)] byte[] secret,
            [MarshalAs(UnmanagedType.LPArray)] byte[] unix_timestamp,
            [MarshalAs(UnmanagedType.LPArray)] byte[] request_body,
            [MarshalAs(UnmanagedType.LPArray)] byte[] outMsg);

        /// <summary>
        /// 输入数据字符串
        /// </summary>
        private byte[] bDataIn = new byte[1024];

        public string DataIn
        {
            get { return System.Text.Encoding.Default.GetString(bDataIn); }
            set { bDataIn = System.Text.Encoding.Default.GetBytes(value); }
        }

        /// <summary>
        /// 输出数据字符串
        /// </summary>
        private byte[] bDataOut = new byte[1024];

        public string DataOut
        {
            get { return System.Text.Encoding.Default.GetString(bDataOut); }
            set { bDataOut = System.Text.Encoding.Default.GetBytes(value); }
        }

        public CardFormat ReadIC(string readType)
        {
            DataIn = readType;
            ZJ_ReadCardInfo_NoPin(bDataIn, bDataOut);
            CardReadOut cardReadOut = JsonConvert.DeserializeObject<CardReadOut>(DataOut);
            if (!cardReadOut.Success)
            {
                throw new Exception("读卡失败:" + cardReadOut.Message);
            }
            else
            {
                CardFormat data = new CardFormat();
                ReadCardInfo readCardInfo = JsonConvert.DeserializeObject<ReadCardInfo>(DataOut);
                data.KaHao = readCardInfo.CardNum;
                data.CanBaoDiXzqhdm = readCardInfo.ylbxtcqbm;
                data.KaShiBieMa = readCardInfo.CardIDCode;
                data.ShenFenZh = readCardInfo.IDNum;
                data.XingMing = readCardInfo.Name;
                data.XingBie = readCardInfo.Gender;
                data.PsamKaHaoZdjbh = readCardInfo.PsamTN;
                data.Flag = readCardInfo.flag;
                return data;
            }
        }
    }
}